﻿Ext.namespace('iOS.Bar');

/*
 * cardView上的bar的抽象类
 */
Ext.define('ToolBar', {
	extend : 'BaseBar',

    statics : {
        BaseCls    : 'tool-bar',
        ToolbarBtn : 'toolbar-btn',
        ArrowBtn   : 'arrow-btn',
        RectBtn    : 'rect-btn',
        LeftBtn    : 'left-btn',
        RightBtn   : 'right-btn',
        BarTitle   : 'bar-title',
        BtnContent : 'btn-content',
        ReplaceBtn : 'replace-btn'
    },

    _eventList: [
        ['changeBtnBar'],
        ['addEvt'],
        ['hideToolBar'],
        ['showToolBar']
    ],

    _curCardBtnCfg: null,

    _hideType: null,

    curCard: null,

    /*
     * 本card中所有按钮容器集
     * cardName: btnBar
     */
    btnBars: {},

    hideToolBar: function(type){
        type = type || 'fade';
        if(type == 'fade'){
            this.getElCache().css('opacity', '0');    
        }
        this._oriZIndex = this.getElCache().css('zIndex');
        this.getElCache().on('webkitTransitionEnd', function(){
            this.style.webkitTransitionDelay = '0';
           $(this).css('zIndex', '0').off('webkitTransitionEnd');
        });
        this._hideType = type;
    },

    showToolBar: function(){
        var type = this._hideType;
        this.getElCache().css({
            'zIndex' : this._oriZIndex,
            'opacity': '1'
        }).on('webkitTransitionEnd', function(){
            this.style.webkitTransitionDelay = '100ms';
            $(this).off('webkitTransitionEnd');
        });
    },

    changeBtnBar: function(tarCard, direction, aminType){
        var curBtnBar = this.btnBars[this.curCard].getElCache(),
            tarBtnBar = this.btnBars[tarCard].getElCache(),
            rplBtnCls = this.self.ReplaceBtn,
            rgtBtnCls = this.self.RightBtn,
            aminType  = aminType || 'slide',
            curEls, tarEls, curTransEl, tarTransEl,
            curStyle  = {}, 
            tarStyle  = {};
        curEls        = curBtnBar.children();
        tarEls        = tarBtnBar.children();
        curTransEl    = curEls.not('.' + rgtBtnCls + ', .' + rplBtnCls);
        tarTransEl    = tarEls.not('.' + rgtBtnCls + ', .' + rplBtnCls);
        tarEls.css('opacity', '0');
        tarStyle  = {
            'opacity'          : '1'
        };
        curStyle  = {
            'opacity'          : '0'
        };
        tarBtnBar.show();
        
        if(aminType == 'slide'){
            tarTransEl.css('-webkit-transform', getTrans(direction == 'right'?'-':''));
            tarStyle['-webkit-transform'] = 'translate3d(0, 0, 0)';
            curStyle['-webkit-transform'] = getTrans(direction == 'right'?'':'-');
        }
        setTimeout(function(){
            curEls.css('opacity', '0');
            tarEls.css('opacity', '1');
            curTransEl.css(curStyle);
            tarTransEl.css(tarStyle);
        }, 1);
        curTransEl.eq(1).on('webkitTransitionEnd', function(){
            curBtnBar.hide();
            curTransEl.css('-webkit-transform', 'translate3d(0, 0, 0)');
            $(this).off('webkitTransitionEnd');
        });
        this.curCard = tarCard;
        function getTrans(d){
            return 'translate3d(' + d + System.width / 2 * 0.9 + 'px, 0, 0)'
        }
    }
});

iOS.Bar.ToolBar = ToolBar;